import errorEvent from '../../errorMessage';
import store from '@/store/index';
const uuid = require('uuid');


export default {
    async getAll() {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceId = store.state.mainOptions.service.serviceId;
        var allServiceCache = `SELECT * FROM local_api_cache WHERE project_id = '${projectId}' AND service_id = '${serviceId}'`;
        var allProjectCache = `SELECT * FROM project_cache WHERE project_id = '${projectId}' AND disable = 0`;
        try {
            let ServiceCache = await window.DB.find(allServiceCache);
            let ProjectCache = await window.DB.find(allProjectCache);
            return {
                res: ProjectCache,
                localRes: ServiceCache,
            };
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
    async creat(param) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceId = store.state.mainOptions.service.serviceId;
        var id = uuid.v1();
        var CT = new Date().getTime();
        var projectSql = `INSERT INTO project_cache (
            cache_id,
            project_id,
            key,
            name,
            disable,
            update_time, 
            create_time
            ) VALUES (
            '${id}',
            '${projectId}',
            '${param.key.replace(/\'/g, "\'\'")}',
            '${param.name.replace(/\'/g, "\'\'")}',
            0,${CT}, ${CT});`;
        var serviceSql = `INSERT INTO local_api_cache (
                cache_id,
                project_id,
                service_id,
                key,
                value,
                update_time, 
                create_time
                ) VALUES (
                '${uuid.v1()}',
                '${projectId}',
                '${serviceId}',
                '${param.key.replace(/\'/g, "\'\'")}',
                '${param.value.replace(/\'/g, "\'\'")}',
                ${CT}, ${CT});`;
        try {
            await window.DB.creat(projectSql);
            await window.DB.creat(serviceSql);
            return true;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
    async update(param) {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceId = store.state.mainOptions.service.serviceId;
        var CT = new Date().getTime();
        try {
            var getCache = `SELECT * FROM local_api_cache WHERE key = '${param.key}' AND project_id = '${projectId}' AND service_id = '${serviceId}';`
            var cacheRes = await window.DB.find(getCache);
            if (cacheRes.length) {
                var sql = `
                    UPDATE local_api_cache SET 
                    value = '${param.value.replace(/\'/g, "\'\'")}',
                    update_time = ${CT}
                    WHERE key = '${param.key}' AND project_id = '${projectId}' AND service_id = '${serviceId}';`;
                await window.DB.update(sql);
                return true;
            } else {
                var serviceSql = `INSERT INTO local_api_cache (
                    cache_id,
                    project_id,
                    service_id,
                    key,
                    value,
                    update_time, 
                    create_time
                    ) VALUES (
                    '${uuid.v1()}',
                    '${projectId}',
                    '${serviceId}',
                    '${param.key.replace(/\'/g, "\'\'")}',
                    '${param.value.replace(/\'/g, "\'\'")}',
                    ${CT}, ${CT});`;
                await window.DB.creat(serviceSql);
                return true;
            };
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
    async delete(id) {
        var CT = new Date().getTime();
        var sql = `
        UPDATE project_cache SET 
        disable = 1,
        update_time = ${CT}
        WHERE cache_id = '${id.id}';`;
        try {
            await window.DB.update(sql);
            return true;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
}